//
// Created by ZLY on 2025/5/9.
//

#include "stats.h"

void Stats::update(float dt) {
    Object::update(dt);
    regenMana(dt);
    if (is_invincible_) {
        invincible_timer_ += dt;
        if (invincible_timer_ > invincible_time_) {
            is_invincible_ = false;
            invincible_timer_ = 0.0f;
        }
    }
}

void Stats::regenMana(float dt) {
    mana_ += mana_regen_ * dt;
    if (mana_ > max_mana_) {
        mana_ = max_mana_;
    }
}

bool Stats::canUseMana(float mana_cost) const {
    return mana_ > mana_cost;
}

void Stats::takeDamage(float damage) {
    if (is_invincible_) {
        return;
    }
    health_ -= damage;
    SDL_Log("Actor:%s took %f damage", "精灵", damage);
    if (health_ <= 0) {
        health_ = 0;
        is_alive_ = false;
    }
    is_invincible_ = true;
    invincible_timer_ = 0.0f;
}

void Stats::useMana(float cost) {
    mana_ -= cost;
    if (mana_ < 0) {
        mana_ = 0;
    }
}

Stats *Stats::addStatsChild(Actor *parent, float max_health, float max_mana, float damage, float mana_regen) {
    auto stats = new Stats();
    stats->parent_ = parent;
    stats->health_ = max_health;
    stats->max_health_ = max_health;
    stats->mana_ = max_mana;
    stats->max_mana_ = max_mana;
    stats->damage_ = damage;
    stats->mana_regen_ = mana_regen;
    stats->is_alive_ = true;
    parent->addChild(stats);
    return stats;
}
